using System;
using System.Collections.Generic;
using System.Text;

namespace NBusiness.CodeDom
{
    [Serializable]
    public abstract class EntityField : EntityCompileUnit, IHasParent, IValidatable
	{
		#region Member properties and fields
		private string _Name = null;
		public string Name
		{
			get { return _Name; }
			set { _Name = value; }
		}

		private Type _Type = null;
		public Type Type
		{
			get { return _Type; }
			set { _Type = value; }
		}

		private bool _IsId = false;
		public bool IsId
		{
			get { return _IsId; }
			set { _IsId = value; }
		}

		private bool _IsAuto = false;

		public bool IsAuto
		{
			get { return _IsAuto; }
			set { _IsAuto = value; }
		}

        private bool _IsNullable = false;

        public bool IsNullable
        {
            get { return _IsNullable; }
            set { _IsNullable = value; }
        }

        private bool _isReadOnly = false;
        public bool IsReadOnly
        {
            get { return _isReadOnly; }
            set { _isReadOnly = value; }
        }
        private Entity _parent;
        public IHasParent Parent
        {
            get { return _parent; }
            set { _parent = (Entity)value; }
        }
		#endregion

		#region Helpers
        public static string[] GetNames(Dictionary<EntityField, EntityField>.KeyCollection Fields)
        {
            EntityField[] f = new EntityField[Fields.Count];
            Fields.CopyTo(f, 0);
            return GetNames(f);
        }
        public static string[] GetNames(EntityField[] Fields)
		{
			List<string> names = new List<string>();
			foreach (EntityField f in Fields)
			{
				names.Add(f.Name);
			}
			return names.ToArray();
		}
		public static string[] GetIdNames(EntityField[] Fields)
		{
			List<string> names = new List<string>();
			foreach (EntityField f in Fields)
			{
				if (f.IsId) names.Add(f.Name);
			}
			return names.ToArray();
		}
		#endregion

		#region object overrides
		public override bool Equals(object obj)
		{
			if (!(obj is EntityField)) return false;

			return Name.Equals((obj as EntityField).Name);
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		public override string ToString()
		{
			return Name;
		} 
		#endregion
	}
}
